package com.one.group.admin.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.one.group.admin.mapper.HomeMapper;
import com.one.group.enums.ErrorCode;
import com.one.group.model.vo.*;
import com.one.group.result.Result;
import com.one.group.service.HomeService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/home")
@Tag(name = "首页接口")
public class HomeController {
    @Autowired
    private HomeMapper homeMapper;
    @Autowired
    private HomeService homeService;

    @RequestMapping(value = "/countBar", method = RequestMethod.GET)
    public Result<HomeCountBarVo> countBar() {
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
        HomeCountBarVo homeCountBar = homeMapper.getHomeCountBar(tenantId);
        if(homeCountBar != null){
            return ResultUtil.success(homeCountBar);
        }
        return ResultUtil.error(ErrorCode.UPDATE_ERROR.getCode(), "获取数据失败");
    }

    @RequestMapping(value = "/houseCount", method = RequestMethod.GET)
    public Result<HomeHouseCountVo> houseCount() {
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
        HomeHouseCountVo houseCount = homeMapper.getHouseCount(tenantId);
        if(houseCount == null){
            return ResultUtil.error(ErrorCode.UPDATE_ERROR.getCode(), "获取数据失败");
        }
        houseCount.setVacantHouses(houseCount.getTotalHouses() - houseCount.getOccupiedHouses()- houseCount.getRentedHouses());
        return ResultUtil.success(houseCount);
    }

    @RequestMapping(value = "/head", method = RequestMethod.GET)
    public Result<HomeHeadVo> head() {
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));

        HomeHeadVo homeHead = homeMapper.getHomeHead(tenantId);
        if(homeHead == null){
            return ResultUtil.error(ErrorCode.UPDATE_ERROR.getCode(), "获取数据失败");
        }
        return ResultUtil.success(homeHead);
    }

    @RequestMapping(value = "/homePrice", method = RequestMethod.GET)
    public Result<HomePriceVo> price() {
        return ResultUtil.success(homeService.getHomePrice());
    }
    @RequestMapping(value = "/homeList", method = RequestMethod.GET)
    public Result<List<HomeListVo>> list() {
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
        return ResultUtil.success(homeMapper.getHomeList(tenantId));
    }
}
